package control

import "github.com/enriquebris/goconcurrentqueue"
import "sync"

// ControlQueue 存储控制请求，以队列形式存储
type ControlQueue struct {
	m sync.Map
	//q *goconcurrentqueue.FIFO
}

func NewControlQueue() *ControlQueue {
	return &ControlQueue{}
}

func (c *ControlQueue) Push(deviceCode string, channel chan string) {
	q, _ := c.m.LoadOrStore(deviceCode, goconcurrentqueue.NewFIFO())
	q.(*goconcurrentqueue.FIFO).Enqueue(channel)
}

func (c *ControlQueue) Pop(deviceCode string) chan string {
	q, ok := c.m.Load(deviceCode)
	if ok {
		item, err := q.(*goconcurrentqueue.FIFO).Dequeue()
		if err != nil {
			return nil
		}
		return item.(chan string)
	}
	return nil
}

func (c *ControlQueue) IsEmpty(deviceCode string) bool {
	q, ok := c.m.Load(deviceCode)
	if ok {
		return q.(*goconcurrentqueue.FIFO).GetLen() == 0
	}
	return true
}
